package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Attention;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IAttentionService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 *@Discription   新增用户关注列表

 *@Params

 *@Return

 *@Authod  黄照伟

 *@CreationTime  2019/3/12 12:59

 *@change

 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private IAttentionService iAttentionService;
    @PostMapping("")
    public Object register(User user) {
        JSONResult json = new JSONResult();
        try {
            userService.register(user);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @PutMapping("")
    public Object update(User user) {
        JSONResult json = new JSONResult();
        try {
            userService.update(user);
            json.setData(userService.selectByUserId(user.getId()));
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @GetMapping("/{authorId}/travels")
    public Object travel(TravelQueryObject qo) {
        PageInfo data = travelService.selectTravelById(qo);

        return data;
    }


    //点评头条, 个人页面
    @GetMapping("/strategyComment")
    public Object strategyComment(StrategyCommentQueryObject qo) {
        PageInfo data = commentService.strategyComment(qo);
        return data;
    }

    //查询评论人
    @GetMapping("/{userId}")
    public Object get(@PathVariable Long userId) {
        User user = userService.selectByUserId(userId);
        return user;
    }

    //查看是否被关注
    @GetMapping("/isFocus/{loginUserId}/{userId}")
    public Object isFocus(@PathVariable("loginUserId") Long loginUserId, @PathVariable("userId")Long userId ) {
        JSONResult result = new JSONResult();
        Attention attention = iAttentionService.get(loginUserId, userId);
        if (attention != null) {
            result.setSuccess(true);
            result.setData(attention);
        } else {
            result.setSuccess(false);
        }
        return result;
    }

    //取消个人页面的关注
    @DeleteMapping("/deleteFocus/{loginUserId}/{userId}")
    public Object deleteFocus(@PathVariable("loginUserId") Long loginUserId, @PathVariable("userId") Long userId) {
        JSONResult result = new JSONResult();
        iAttentionService.deleteFocus(loginUserId,userId);
        //返回被关注人的个人信息
        try {
            Attention attention = iAttentionService.get(loginUserId, userId);
            result.setData(attention);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("网络异常,操作失败");
        }
        return result;
    }

    //对评论详情中的个人进行关注
    @PostMapping("/insertFocus/{loginUserId}/{userId}")
    public Object insertFocus(@PathVariable("loginUserId") Long loginUserId,@PathVariable("userId")Long userId) {
        JSONResult result = new JSONResult();
        try {
            iAttentionService.insertFocus(loginUserId,userId);
            Attention attention = iAttentionService.get(loginUserId, userId);
            result.setData(attention);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("网络异常,操作失败");
        }


        return result;
    }


}
